//
//  WelcomeController.swift
//  SinaMicroblog
//
//  Created by 唐绍禹 on 15/8/3.
//  Copyright © 2015年 唐绍禹. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeController: UIViewController {

    private var IconConstrainY: NSLayoutConstraint?
    override func viewDidLoad() {
        super.viewDidLoad()

        prepareUI()
        
        //加载用户头像
        if let urlString = UserAccount.loadAccount()?.avatar_large {
            iconView.sd_setImageWithURL(NSURL(string: urlString)!)
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        IconConstrainY?.constant = UIScreen.mainScreen().bounds.height - IconConstrainY!.constant
        
        //动画
        UIView.animateWithDuration(1.2, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 15.0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //更新约束
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                //根据透明度改变文字文字出来的效果
                UIView.animateWithDuration(0.6, delay: 0.0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
                    self.label.alpha = 1.0
                    }, completion: { (_) -> Void in
                        NSThread.sleepForTimeInterval(1.0)
                        NSNotificationCenter.defaultCenter().postNotificationName(TurnToMainContoller, object: true)
                })
        }
    }
    
    private func prepareUI() {
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(label)
        
        //添加约束
        //背景图片
        backImageView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview":backImageView]))
       
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview":backImageView]))
        //图像
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 200))
        //记录约束
        IconConstrainY = view.constraints.last
        
        iconView.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        iconView.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        
        //设置标签约束
        label.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))

        label.alpha = 0
        
    }
    
    //懒加载控件
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView: UIImageView = {
        
        let iv = UIImageView (image: UIImage(named: "avatar_default_big"))
        iv.layer.masksToBounds = true
        iv.layer.cornerRadius = 45
        
        return iv
    }()
    private lazy var label:UILabel = {
        
        let label = UILabel()
        label.textColor = UIColor(white: 0.3, alpha: 1.0)
        label.text = "欢迎归来"
        label.sizeToFit()
    
        return label
    }()

}
